package com.songshuai.webapi.baseapi.service.impl;

import com.songshuai.webapi.baseapi.dao.YixueBaseCampusCrmMapper;
import com.songshuai.webapi.baseapi.dao.YixueBaseCampusMapper;
import com.songshuai.webapi.baseapi.dao.YixueBaseCampusSchoolMapper;
import com.songshuai.webapi.baseapi.dao.YixueBaseSchoolMapper;
import com.songshuai.webapi.baseapi.dto.crmbase.CampusListItemDto;
import com.songshuai.webapi.baseapi.dto.request.CampusListReqDto;
import com.songshuai.webapi.baseapi.dto.request.CampusUpdateOpenStatusReqDto;
import com.songshuai.webapi.baseapi.dto.response.CampusCrmAccountResDto;
import com.songshuai.webapi.baseapi.dto.response.CampusListResDto;
import com.songshuai.webapi.baseapi.dto.response.CampusResDto;
import com.songshuai.webapi.baseapi.dto.response.Tpo_Sys_User;
import com.songshuai.webapi.baseapi.model.YixueBaseCampus;
import com.songshuai.webapi.baseapi.model.YixueBaseCampusCrm;
import com.songshuai.webapi.baseapi.service.CampusService;
import com.songshuai.webapi.baseapi.service.UserService;
import com.songshuai.webapi.baseapi.utils.PropertiesUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class CampusServiceImpl implements CampusService {

    @Autowired
    private YixueBaseCampusMapper campusMapper;

    @Autowired
    private YixueBaseSchoolMapper schoolMapper;

    @Autowired
    private YixueBaseCampusCrmMapper campusCrmMapper;

    @Autowired
    private YixueBaseCampusSchoolMapper campusSchoolMapper;

    @Autowired
    private UserService userService;

    @Override
    public List<CampusListResDto> getCampusList(CampusListReqDto parmDto) {
        List<CampusListResDto> resDtosList = new ArrayList<>();
        List<YixueBaseCampus> campusList = campusMapper.selectListByPage(parmDto.getName(),
                parmDto.getOpenTimeStart(),parmDto.getOpenTimeEnd(),parmDto.getOpenStatus(),
                parmDto.getIsLinkage(),parmDto.getCanReception(),
                parmDto.getUseStatus(),parmDto.getSchoolId());
        for (YixueBaseCampus campus:campusList) {
            CampusListResDto resDto = new CampusListResDto();
            PropertiesUtil.copyProperties(campus,resDto);
            resDto.setCampusId(campus.getId());
            List<String> strings = schoolMapper.selectSchoolNameByCampus(campus.getId());
            resDto.setSchoolList(strings);
            resDto.setUseStatus(campus.getIsDelete());
            resDtosList.add(resDto);
        }
        return resDtosList;
    }

    /**
     * 根据校区名称获取校区列表
     * @param campusName
     * @param size
     * @return
     */
    @Override
    public List<CampusListItemDto>getCampusByName(String campusName,Integer size){
        List<CampusListItemDto> retList=new ArrayList<>();
        if(campusName==null){
            return retList;
        }
        if(size==null){
            size=50;
        }
        List<YixueBaseCampus> baseCampuses= campusMapper.selectCampusByName(campusName,size);
        if(baseCampuses==null||baseCampuses.size()==0){
            return retList;
        }
        for (YixueBaseCampus campus:baseCampuses){
            CampusListItemDto itemDto=new CampusListItemDto();
            itemDto.setCampusId(campus.getId());
            itemDto.setName(campus.getName());
            retList.add(itemDto);
        }
        return retList;
    }

    @Override
    @Transactional
    public Boolean insertSelective(YixueBaseCampus record ,List<Integer> crmIds) {
        Date date = new Date();
        int a = 0;
        record.setCreateTime(date);
        record.setUpdateTime(date);
        a = campusMapper.insertSelective(record);
        for (Integer crmId:crmIds) {
            if(null != crmId && crmId > 0){
                YixueBaseCampusCrm entity = new YixueBaseCampusCrm();
                entity.setCampusId(record.getId());
                entity.setCrmAccountId(crmId);
                entity.setCreateTime(date);
                entity.setUpdateTime(date);
                a = campusCrmMapper.insertSelective(entity);
            }
        }
        return a>0;
    }

    @Override
    @Transactional
    public void deleteCampus(Integer campusId){
        Date date = new Date();
        campusMapper.updateByPrimaryKeySelective(new YixueBaseCampus(){{setId(campusId);setIsDelete(true);setUpdateTime(date);}});
        //如果把关联关系也删除，则查询已删除的校区，学校和关联账号查不出来
//        campusSchoolMapper.removeByCampusId(campusId);
//        campusCrmMapper.removeByCampusId(campusId);
    }


    @Override
    public void updateOpenStatus(CampusUpdateOpenStatusReqDto reqDto) {
        campusMapper.updateByPrimaryKeySelective(new YixueBaseCampus(){{setId(reqDto.getCampusId());setOpenStatus(reqDto.getOpenStatus());setUpdateTime(new Date());}});
    }

    @Override
    @Transactional
    public void updateCampusByid(YixueBaseCampus record ,List<Integer> crmIds) {
        Date date = new Date();
        record.setUpdateTime(date);
        campusMapper.updateByPrimaryKeySelective(record);
        campusCrmMapper.deleteByCampusId(record.getId());
        for (Integer crmId: crmIds) {
            YixueBaseCampusCrm entity = new YixueBaseCampusCrm();
            entity.setCampusId(record.getId());
            entity.setCrmAccountId(crmId);
            entity.setCreateTime(date);
            entity.setUpdateTime(date);
            campusCrmMapper.insertSelective(entity);
        }

    }



    @Override
    public CampusResDto queryCampusById(Integer campusId) {
        YixueBaseCampus yixueBaseCampus = campusMapper.selectByPrimaryKey(campusId);
        List<YixueBaseCampusCrm> yixueBaseCampusCrms = campusCrmMapper.selectListByCampusId(campusId);
        CampusResDto result = new CampusResDto();
        PropertiesUtil.copyProperties(yixueBaseCampus,result);
        result.setCampusId(yixueBaseCampus.getId());

        if(null != yixueBaseCampusCrms && yixueBaseCampusCrms.size()>0){
            //获取所有crm账号id
            List<Integer> crmAccountIds = yixueBaseCampusCrms.stream().map(crms -> crms.getCrmAccountId()).distinct().collect(Collectors.toList());
            List<Tpo_Sys_User> users = userService.getUsers(crmAccountIds);
            if (null != users && users.size()>0){
                List<CampusCrmAccountResDto> crmInfos = users.stream().map(user -> {
                    CampusCrmAccountResDto crmAccount = new CampusCrmAccountResDto();
                    crmAccount.setCrmAccountId(user.getUserID());
                    crmAccount.setName(user.getTrueName());
                    return crmAccount;
                }).collect(Collectors.toList());
                result.setCrmInfos(crmInfos);
            }
        }

        return result;
    }



}
